<?php
function contact_default(){
    //
    global $CONTACT;
    includeModule('static_content');    
    $url = getCurrentRequestParameters('url');

    require_once(REAL_PATH . "/classes/PFBC/Form.php");

    $contentMapper = new staticContent_Mapper();
    $obj = $contentMapper->findById(CONTACT_ID);
    $tpl = XTemplate::getInstance();

    $form = new Form("contactForm",500);
    $requestUri = SERVER_PATH.$_SERVER['REQUEST_URI'];
    if($requestUri != @$_SERVER['HTTP_REFERER']){
        $form->clearValues("contactForm");
        $form->clearErrors("contactForm");
    }

    $form->configure(array(
        "method" => "post",
        "action" => ""
    ));

   $form->addElement(new Element_Hidden("form_trigger","1"));

    $form->addElement(new Element_Textbox("Nume:", "nume", array(
        'validation' => array(new Validation_Required)
    )));

    $form->addElement(new Element_Textbox("Prenume:", "prenume", array(
        'validation' => array(new Validation_Required)
    )));

    $form->addElement(new Element_Textbox("Telefon:", "telefon", array(
        'validation' => array(new Validation_Required)
    )));

    $form->addElement(new Element_Textbox("Adresa de email:", "mail", array(
        'validation' => array(new Validation_Required, new Validation_Email)
    )));

    $form->addElement(new Element_Textarea("Mesaj:", "mesaj", array(
        'validation' => array(new Validation_Required)
    )));

     $form->addElement(new Element_Button('Trimite'));
    /*$form->addElement(new Element_Captcha(
        
    ));*/    
    if(isset ($_POST['form_trigger']) && $_POST['form_trigger'] == 1){
         if(Form::isValid("contactForm")) {
             $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                // Additional headers
             $body = "Date contact:<br><be> ";
             $body .= "&nbsp;Nume: ".$_POST['nume']."<br>";
             $body .= "&nbsp;Prenume: ".$_POST['prenume']."<br>";
             $body .= "&nbsp;Telefon: ".$_POST['telefon']."<br>";
             $body .= "&nbsp;Mail: ".$_POST['mail']."<br>";
             $body .= "Mesaj<br>";
             $body .= "&nbsp;".$_POST['mesaj'];             
            @mail(MAIL_MESAj_CONTACT, 'psihologos.ro - mesaj formular contact', $body, $headers);
            redirectToUrl(SERVER_PATH."/contact-cabinet-psihologie-psihoterapie/?message_sent=1");
        
        }
    }

    $template = "modules/static_content/static_module.html";
    $body = str_replace('{FORM}', $form->render(true), $obj->getBody());
    $message = "";
    if(isset ($_GET['message_sent']) && $_GET['message_sent'] == 1){
        $message = "<p style='color: green;font-weight:bold'>Mesajul a fost trimis. Va multumim.</p>";
      
    }

    $body = str_replace('{message}', $message, $body);
    $body = str_replace(array('{CONTACT_ADRESA}', '{CONTACT_EMAIL1}', '{CONTACT_EMAIL2}','{CONTACT_TELEFON}'),
        array($CONTACT['CONTACT_ADRESA'], $CONTACT['CONTACT_EMAIL1'], $CONTACT['CONTACT_EMAIL2'],$CONTACT['CONTACT_TELEFON']),
        $body);
    $tpl->assign_file('CONTENT',$template);
    $tpl->assign('moduleTitle', $obj->getTitle());
    $tpl->assign('moduleContent', $body);
    $tpl->parse('main.module_title');

}
?>
